package com.example.smartcity_40.lawyer;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.smartcity_40.R;
import com.example.smartcity_40.lawyer.apter.UserevaluateApter;
import com.example.smartcity_40.lawyer.bean.LawyerBean;
import com.example.smartcity_40.lawyer.bean.UserevaluateBean;
import com.example.smartcity_40.tool.Tool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class LawyerdetailsActivity extends AppCompatActivity {

    private LawyerdetailsActivity context;
    private String lawyerid;
    private RelativeLayout service;
    private RecyclerView user_evaluate;
    private Tool tool;
    private ImageView avatarUrl;
    private TextView name;
    private TextView legalExpertiseName;
    private TextView serviceTimes;
    private TextView favorableCount;
    private TabLayout mytab;
    private ImageView certificateImgUrl;
    private TextView workStartAt;
    private TextView licenseNo;
    private TextView baseInfo;
    private TextView eduInfo;
    private UserevaluateApter userevaluateApter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lawyerdetails);
        context = this;
        initview();
        getinformationlawyer();

    }

    private void userevaluatemethod() {
        tool.send("/prod-api/api/lawyer-consultation/lawyer/list-evaluate?lawyerId="+lawyerid+"&pageNum=1&pageSize=10",
                Tool.GET,
                null,
                true,
                UserevaluateBean.class,
                new Function1<UserevaluateBean, Unit>() {
                    @Override
                    public Unit invoke(UserevaluateBean userevaluateBean) {
                        if (userevaluateBean.getCode() == 200) {
                            user_evaluate.setLayoutManager(new LinearLayoutManager(context));
                            userevaluateApter.setDate(userevaluateBean.getRows());
                            user_evaluate.setAdapter(userevaluateApter);
                        }
                        return null;
                    }
                });
    }

    private void lawyerandusermethod(LawyerBean.DataBean data) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("服务方式");
        strings.add("用户评价");

        for (String string : strings) {
            TabLayout.Tab tab = mytab.newTab();
            tab.setText(string);
            mytab.addTab(tab);
        }

        mytab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String type = tab.getText().toString();
                if(strings.get(0).equals(type)){
                    service.setVisibility(View.VISIBLE);
                    user_evaluate.setVisibility(View.GONE);
                    initlawyerinformation(data);
                }else  if(strings.get(1).equals(type)){
                    service.setVisibility(View.GONE);
                    user_evaluate.setVisibility(View.VISIBLE);
                    userevaluatemethod();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initlawyerinformation(LawyerBean.DataBean data) {

        try {
           String dataAvatarUrl = data.getCertificateImgUrl();
            if(TextUtils.isEmpty(dataAvatarUrl)){
                certificateImgUrl.setImageResource(R.drawable.waimai);
            }else {
                Glide.with(context).load(Tool.url+dataAvatarUrl)
                        .error(getDrawable(R.drawable.waimai))
                        .into(certificateImgUrl);
            }


            String dataName = data.getWorkStartAt();
            if(!TextUtils.isEmpty(dataName)){
                workStartAt.setText("工作年限: "+dataName);
            }

            String dataLicenseNo = data.getLicenseNo();
            if(!TextUtils.isEmpty(dataLicenseNo)){
                licenseNo.setText("执业证编号: "+dataLicenseNo);
            }

            String dataBaseInfo = data.getBaseInfo();
            if(!TextUtils.isEmpty(dataBaseInfo)){
                baseInfo.setText("律师简介: "+dataBaseInfo);
            }

            String dataEduInfo = data.getEduInfo();
            if(!TextUtils.isEmpty(dataEduInfo)){
                eduInfo.setText("律师教育背景: "+dataEduInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void getinformationlawyer() {
        tool.send("/prod-api/api/lawyer-consultation/lawyer/"+lawyerid,
                Tool.GET,
                null,
                true,
                LawyerBean.class,
                new Function1<LawyerBean, Unit>() {
                    @Override
                    public Unit invoke(LawyerBean lawyerBean) {
                        if (lawyerBean.getCode() == 200) {
                            initlawyer(lawyerBean.getData());
                            lawyerandusermethod(lawyerBean.getData());
                            initlawyerinformation(lawyerBean.getData());
                        }
                        return null;
                    }
                });
    }

    private void initlawyer(LawyerBean.DataBean data) {
        try {
            if(data ==null){
                return;
            }
            String dataAvatarUrl = data.getAvatarUrl();
            if(!TextUtils.isEmpty(dataAvatarUrl)){
                Glide.with(context).load(Tool.url+dataAvatarUrl).into(avatarUrl);
            }else {
                Glide.with(context).load(Tool.url+dataAvatarUrl)
                        .error(R.drawable.waimai)
                        .into(avatarUrl);
            }

            String dataName = data.getName();
            if(!TextUtils.isEmpty(dataName)){
                name.setText(dataName);
            }

            String dataLegalExpertiseName = data.getLegalExpertiseName();
            if(!TextUtils.isEmpty(dataLegalExpertiseName)){
                legalExpertiseName.setText("专长: "+dataLegalExpertiseName);
            }

            Integer dataServiceTimes = data.getServiceTimes();
            if(dataServiceTimes !=null ){
                serviceTimes.setText("咨询人数： "+String.valueOf(dataServiceTimes));
            }


            Integer dataFavorableCount = data.getFavorableCount();
            if(dataFavorableCount !=null ){
                favorableCount.setText("服务次数： "+String.valueOf(dataFavorableCount));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initview() {
        Intent intent = getIntent();
        lawyerid = intent.getStringExtra("lawyerid");

        tool = new Tool(context);
        userevaluateApter = new UserevaluateApter(context);

        ImageView back = findViewById(R.id.back);
        certificateImgUrl = findViewById(R.id.certificateImgUrl);
        workStartAt = findViewById(R.id.workStartAt);
        licenseNo = findViewById(R.id.licenseNo);
        baseInfo = findViewById(R.id.baseInfo);
        eduInfo = findViewById(R.id.eduInfo);



        avatarUrl = findViewById(R.id.avatarUrl);
        name = findViewById(R.id.name);
        legalExpertiseName = findViewById(R.id.legalExpertiseName);
        serviceTimes = findViewById(R.id.serviceTimes);
        favorableCount = findViewById(R.id.favorableCount);

        mytab = findViewById(R.id.mytab);
        service = findViewById(R.id.service);
        user_evaluate = findViewById(R.id.User_Evaluate);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });
    }
}

/*
* 上部显示头像、名称、法律专长、咨询人数和服务次数
* 下部显示两个 tab 页，分别是服务方式和用户评
* 点击服务方式，展示律师基本信息、律师个人简介和律师证展示照片，
* 律师基本信息包括教育背景、从业年限和执业证号
*
*
*

* */